package com.intech.android.demo.cms.dao.impl;


import com.intech.android.demo.cms.bo.TextContent;
import com.intech.android.demo.cms.common.Constants;
import com.intech.android.demo.cms.common.Util;
import com.intech.android.demo.cms.dao.interfaces.TextContentDAO;
import com.intech.android.demo.cms.enums.Status;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.List;

public class TextContentHibernateDAO extends CommonDAO implements TextContentDAO
{
    public TextContent getById(Integer id)
    {
        return (TextContent) session.get(TextContent.class, id);
    }

    public void save(TextContent textContent)
    {
        session.saveOrUpdate(textContent);
    }

    public void delete(TextContent textContent)
    {
        session.delete(textContent);
    }

    public void delete(Integer textContentId)
    {
        TextContent textContent = getById(textContentId);
        session.delete(textContent);
    }

    public List<TextContent> getAll()
    {
        Criteria criteria = session.createCriteria(TextContent.class);
        List list = criteria.list();
        return list;
    }

    public List<TextContent> getAll(Integer rows)
    {
        Criteria criteria = session.createCriteria(TextContent.class).setMaxResults(rows);
        List list = criteria.list();
        return list;
    }

    public List<TextContent> findTextContentsByName(String name)
    {
        Criteria criteria = session.createCriteria(TextContent.class);
        criteria.add(Expression.eq("name", name));
        List<TextContent> filteredTextContent = criteria.list();
        return filteredTextContent;
    }

    public List<TextContent> findTextContentsByAdvertiser(String advertiser)
    {
        Criteria criteria = session.createCriteria(TextContent.class);
        criteria.add(Expression.eq("advertiser", advertiser));
        List<TextContent> filteredTextContent = criteria.list();
        return filteredTextContent;
    }

    public List<TextContent> findTextContents(List<Integer> regionsIds, List<Integer> categoriesId, Status status, Double chosenFrom, Double chosenTo)
    {
        List<TextContent> filtered = new ArrayList<TextContent>();
        Criteria criteria = session.createCriteria(TextContent.class);
        if (!Util.isEmpty(regionsIds))
        {
            criteria.createCriteria("regions").add(Restrictions.in("id", regionsIds));

        }

        if (!Util.isEmpty(categoriesId))
        {
            criteria.createCriteria("category").add(Restrictions.in("id", categoriesId));
        }

        if (status != null)
        {
            if (status == Status.Active)
            {
                criteria.add(Restrictions.ge("budget", Constants.Logic.MIN_BUDGET));
            }
            else if (status == Status.Inactive)
            {
                criteria.add(Restrictions.lt("budget", Constants.Logic.MIN_BUDGET));
            }
        }

        if (chosenFrom != null && chosenTo != null)
        {
            criteria.add(Restrictions.between("budget", chosenFrom, chosenTo));
        }

        filtered = Util.uniqueFilter(criteria.list());
        return filtered;
    }
}
